/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientside;

import java.io.*;
import java.net.*;
import client.network.*;
import com.cse.p2p.common.CommonSettings;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author TuyetNgan
 */
public class DataClientSide extends Thread {

	private static Integer portControl = CommonSettings.CLIENT_DATA_SERVERSOCKET;
	private static Integer maxPeerService = 10;
	private ConcurrentHashMap<Integer, ClientControlTask_Client> downloadLIst;

	public DataClientSide() {
		downloadLIst = new ConcurrentHashMap<Integer, ClientControlTask_Client>();
	}

	public void run() {

		try {
			ServerSocket serverSocket = null;
			boolean listening = true;
			int count = 0;
			try {
				serverSocket = new ServerSocket( portControl );
			}
			catch ( IOException e ) {
				System.err.println( "Could not listen on port: " + portControl );
				System.exit( -1 );
			}
			System.out.println( "Server start control port!" + portControl );
			while ( listening ) {
				new ClientReceiveDataTask( serverSocket.accept(), this.downloadLIst ).start();
				count++;
			}
			serverSocket.close();
		}
		catch ( IOException e ) {
		}

	}
	
	public synchronized void registerNewDownload(int fileID, ClientControlTask_Client thread){
		this.downloadLIst.put( fileID, thread);
	}
}
